import pandas as pd
import altair as alt
from helpers import keywords_reader

class BarChart:
    __days_mapping = {'日': 1, '周': 7, '月': 30, '年': 365}

    @staticmethod
    def plot(time_range, k=10):
        frequencies = keywords_reader.get_recent_top_k_frequencies(BarChart.__days_mapping[time_range], k)
        df = pd.DataFrame(frequencies if frequencies else [('未知词汇', 0)], columns=['词汇', '数量'])
        return BarChart.__get_bar_chart(df)

    @staticmethod
    def __get_bar_chart(df):
        return alt.Chart(df).mark_bar().encode(
            x=alt.X('数量:Q'),
            y=alt.Y('词汇:N', sort='-x'),
            tooltip=['词汇', '数量']
        ).properties(width=800, height=400)
